package com.manlitech.manlichat.service;

import com.manlitech.manlichat.dao.ChatMessageDao;
import com.manlitech.manlichat.dto.ChatMessage;
import com.manlitech.manlichat.pojo.QueryParams;
import com.manlitech.manlichat.pojo.Result;
import com.manlitech.manlichat.util.ResultUtil;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


@Service("chatMessageService")
public class ChatMessageService {
    @Autowired
    ChatMessageDao chatMessageDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from chatMessage where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<ChatMessage> pageQuery = new PageQuery<ChatMessage>(params.getPage(),params.getLimit());
        PageQuery<ChatMessage> query =sqlManager.execute(sqlReady,ChatMessage.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<ChatMessage> list = chatMessageDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Long chatMessageId) {
        ChatMessage chatMessage = chatMessageDao.single(chatMessageId);
        return ResultUtil.success(chatMessage);
    }

    @Transactional
    public Result save(ChatMessage chatMessage) {
        chatMessageDao.insert(chatMessage);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(ChatMessage chatMessage) {
        chatMessageDao.updateById(chatMessage);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(ChatMessage chatMessage) {
        chatMessageDao.updateTemplateById(chatMessage);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Long> ids) {
        for (Long id:ids) {
            chatMessageDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
